from transitions.extensions import HierarchicalMachine
from transitions.extensions import GraphMachine
from src.utils.loguru_func import step_logger


class State_Machine(GraphMachine):
    STATES = ['step00', 'step01', 'step02',  'step03',
              'step04', 'step05', 'step06', 'step07']

    TRANSITIONS = [
        {'trigger': 'step00_to_step01', 'source': 'step00', 'dest': 'step01'},
        {'trigger': 'step01_to_step02', 'source': 'step01', 'dest': 'step02'},
        {'trigger': 'step02_to_step03', 'source': 'step02', 'dest': 'step03'},
        {'trigger': 'step03_to_step04', 'source': 'step03', 'dest': 'step04'},
        {'trigger': 'step04_to_step05', 'source': 'step04', 'dest': 'step05'},
        {'trigger': 'step05_to_step06', 'source': 'step05', 'dest': 'step06'},
        {'trigger': 'step06_to_step07', 'source': 'step06', 'dest': 'step07'},
        {'trigger': 'step07_to_step00', 'source': 'step07', 'dest': 'step00'}
    ]

    def __init__(self, model):
        super().__init__(model=model, states=State_Machine.STATES,
                         transitions=State_Machine.TRANSITIONS, initial='step00')


class Step_Manager:
    def __init__(self,  app, config_file=None):
        self.config_file = config_file

        self.set_global_app(app)

        self.machine = State_Machine(model=self)

    def set_global_app(self, app):
        self.APP = app
        self.APP.STEP_MANAGER = self
        self.MAIN_WINDOW = self.APP.MAIN_WINDOW

    def step_start(self):
        self.step00_to_step01()

    def create_state_diagram(self):
        self.machine.get_graph().draw('my_state_diagram.png', prog='dot')

    def reset_step(self):
        self.to_step00()

    def on_enter_step00(self):
        step_logger.debug("进入step00")
        self.MAIN_WINDOW.active_step_lable(0)
        # self.step00_to_step01()

    def on_enter_step01(self):
        step_logger.debug("进入step01")

        # self.step01_to_step02()
        self.MAIN_WINDOW.active_step_lable(1)

    def on_enter_step02(self):
        step_logger.debug("进入step02")

        self.MAIN_WINDOW.active_step_lable(2)

    def on_enter_step03(self):
        step_logger.debug("进入step03")

        self.MAIN_WINDOW.active_step_lable(3)

    def on_enter_step04(self):
        print("进入step04")

        # self.step04_to_step05()

    def on_enter_step05(self):
        print("进入step05")

        # self.step05_to_step06()

    def on_enter_step06(self):
        print("进入step06")

        # self.step06_to_step07()

    def on_enter_step07(self):
        print("进入step07")

        # self.step07_to_step00()

    # 查看当前所在的step
    def get_current_step(self):
        return self.state


def test_Step_Manager():
    step_manager = Step_Manager(app=None)
    step_manager.step_start()


# 定义状态
states = [
    'standing',
    'walking',
    {
        'name': 'running',
        'children': [
            'normal',
            'sprinting'
        ]
    },
    'jumping'
]

# 定义转换
transitions = [
    ['walk', 'standing', 'walking'],
    ['stop', 'walking', 'standing'],
    ['run', 'walking', 'running'],
    ['sprint', 'running', 'running_sprinting'],
    ['jog', 'running_sprinting', 'running_normal'],
    ['jump', ['standing', 'walking', 'running'], 'jumping'],
    ['land', 'jumping', 'standing']
]

# 创建状态机


class Robot:
    def __init__(self):
        self.machine = HierarchicalMachine(
            model=self,
            states=states,
            transitions=transitions,
            initial='standing',
            ignore_invalid_triggers=True
        )


# 使用示例
def test_HierarchicalMachine():
    robot = Robot()
    print(f"初始状态: {robot.state}")  # 输出: standing

    robot.walk()
    print(f"行走后状态: {robot.state}")  # 输出: walking

    robot.run()
    print(f"跑步后状态: {robot.state}")  # 输出: running_normal

    robot.sprint()
    print(f"冲刺后状态: {robot.state}")  # 输出: running_sprinting

    robot.jump()
    print(f"跳跃后状态: {robot.state}")  # 输出: jumping

    robot.land()
    print(f"落地后状态: {robot.state}")  # 输出: standing
